'use strict';
// 获取数据库索引
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
  //event为客户端上传的参数
	const {
		user_id,
		author_id
	} = event
	
	const user = await db.collection('users').where({user_id:author_id}).get()
	let likemeList = []
	if( user.data[0].likemeList){
		likemeList = user.data[0].likemeList
	}
	
	
	let likeme = null;
	if(likemeList.includes(user_id)){
		likeme = dbCmd.pull(user_id) 
	}else{
		likeme = dbCmd.addToSet(user_id)
	}
	
	await db.collection('users').where({user_id:author_id}).update({
		likemeList : likeme
	})
  //返回数据给客户端
  return {
		code:200,
		msg:'更新成功'
	}
};
